<?php

namespace app\sugar\controller;


use app\model\Work;
use think\facade\Db;

class Index
{

    public function index(): \think\response\View
    {
        return view('index');
    }

    //保存
    public function add()
    {
        Db::transaction(function () {
            Work::whereDay('create_time')->delete();
            $content = input('content');
            $data = array_filter($content, function($value) {
                return !empty($value);
            });
            foreach ($data as $item) {
                Work::create(['content'=> $item]);
            }
        });
        return jsuccess();
    }

    //日报1周报2
    public function get()
    {
        $type = input('type');
        if ($type == 1) {
            $works = Work::whereDay('create_time')
                ->select()->toArray();
            $text = date('Y-m-d');
        } else{
            $works = Work::whereWeek('create_time')
                ->select()->toArray();
            $text = '';
        }
        foreach ($works as $k=> $work) {
            $text .= '<br>'. ($k+1) . '.' . $work['content'];
        }

        return jsuccess($text);
    }
}
